<?php
/**
 * Created by PhpStorm.
 * User: DaveTang
 * Date: 2020/3/3
 * Time: 15:56
 * 用户信息缓存组件
 */

namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Cache\Cache;

class CommentCacheComponent extends Component
{

    public $_cache_config = 'comments';
    public function initialize(array $config)
    {
        parent::initialize($config);
    }

    /**
     * -新增评论状态
     * @param  $content_type  表名
     * @param  $content_id   表id
     */
    public function add($content_type, $content_id = 0, $status = 'show', $msgbox = 'show')
    {
        $data = $this->getVal($content_type, $content_id);
        if(!empty($data)){
            return 'comment_exist';
        }
        $controller = $this->_registry->getController();
        $controller->loadModel('Comments');
        $comment = $controller->Comments->newEntity();
        $comment->content_type = $content_type;
        $comment->content_id = $content_id;
        $comment->status = $status;
        $comment->msgbox = $msgbox;
        $controller->Comments->save($comment);
        return true;
    }

    /**
     * -更新评论状态
     */
    public function update($content_type, $content_id = 0, $status, $msgbox)
    {
        $data = $this->getVal($content_type, $content_id);
        if(empty($data)){
            return 'comment_not_found';
        }
        $controller = $this->_registry->getController();
        $controller->loadModel('Comments');
        $controller->Comments->query()
            ->update()
            ->set(['status'=>$status, 'msgbox'=>$msgbox])
            ->where(['id' => $data['id']])
            ->execute();
        $this->createCache($content_type, $content_id);
        return true;
    }

    /**
     * 获取评论
     * @param  $content_type  表名
     * @param  $content_id   表id
     */
    public function getVal($content_type, $content_id = 0)
    {
        $key = $content_type . '_' . $content_id;
        $data = Cache::read($key, $this->_cache_config);
        return empty($data) ? $this->createCache($content_type, $content_id) : $data;
    }

    /**
     * 创建缓存
     * @param  $content_type  表名
     * @param  $content_id   表id
     */
    public function createCache($content_type, $content_id = 0)
    {
        $key = $content_type . '_' . $content_id;
        $where = ['content_type' => $content_type, 'content_id'=>$content_id];
        $controller = $this->_registry->getController();
        $controller->loadModel('Comments');
        $data = $controller->Comments->find()
            ->where($where)
            ->first();
        if($content_id !== 0 && empty($data)){
            $data = $this->getVal($content_type);
        }
        $cache = empty($data) ? null : ['id'=>$data['id'],'status'=>$data['status'], 'msgbox'=>$data['msgbox']];
        Cache::write($key, $cache, $this->_cache_config);
        return $cache;
    }

    /**
     * 删除评论状态
     * @param  $content_type  表名
     * @param  $content_id   表id
     */
    public function delete($content_type, $content_id)
    {
        $data = $this->getVal($content_type, $content_id);
        if(empty($data)){
            return 'comment_not_exist';
        }
        $controller = $this->_registry->getController();
        $controller->loadModel('Comments');
        $controller->Comments->deleteAll(['id'=>$data['id']]);
        Cache::delete($content_type . '_' . $content_id, $this->_cache_config);
        return true;
    }

    /**
     * 删除评论状态
     * @param  $content_type  表名
     * @param  $content_id   表id
     */
    public function deleteCache($content_type, $content_id)
    {
        $data = $this->getVal($content_type, $content_id);
        if(empty($data)){
            return 'comment_not_exist';
        }
        Cache::delete($content_type . '_' . $content_id, $this->_cache_config);
        return true;
    }

}